import {fetch, post} from '../../../utils/axios'

const state = {
  name: '新增项目',
  dialog: false,
  valid: null,
  showPubcmd: false,
  showDockercmd: false,
  showJavaopt: false,
  showDockeropt: false,
  showBuildParam: false,
  showPath: false,
  parent: {},
  project_nature: {},
  projectType: {},
  status: {},
  publish_type: {},
  build_param: {},
  pub_version: {},
  detection: {},
  env: {},
  owner: {},
  fields: {},
  pubcmd: {},
  dockercmd: {},
  javaopt: {},
  dockeropt: {},
  service: {},
  host: {items: [{ text: '', value: '' }]},
  nameRules: [
    (v) => !!v || '请输入项目名'
  ],
  name_enRules: [
    (v) => !!v || '请输入项目英文名'
  ],
  projectTypeRules: [
    (v) => !!v || '请选择项目类型'
  ],
  envRules: [
    (v) => !!v || '请选择项目环境'
  ],
  gitPath: [
    (v) => !!v || '请填写git地址'
  ],
  pubcmdRules: [
    (v) => { if (state.showPubcmd && !v) { return '请选择发布参数' } return true }
  ],
  javaoptRules: [
    (v) => { if (state.showJavaopt && !v) { return '请选择JAVA参数' } return true }
  ],
  dockercmdRules: [
    (v) => { if (state.showDockercmd && !v) { return '请选择容器发布参数' } return true }
  ],
  dockeroptRules: [
    (v) => { if (state.showDockeropt && !v) { return '请选择容器JAVA参数' } return true }
  ],
  relative_conf_pathRules: [
    (v) => { if (state.showPath && !v) { return '请填写文件路径' } return true }
  ],
  absolute_conf_pathRules: [
    (v) => { if (state.showPath && !v) { return '请填写文件路径' } return true }
  ],
  pubverRules: [
    (v) => v !== undefined || '请选择发布版本'
  ],
  ownerRules: [
    (v) => !!v || '请选择负责人'
  ],
  detectionRules: [
    (v) => v !== undefined || '请选择检测方式'
  ],
  detectionMethodRules: [
    (v) => !!v || '请填写检测地址'
  ],
  msg: {
    name: '',
    name_en: '',
    projectType: '',
    env: ''
  }
}

const mutations = {
  closeNewProject: (state, payload) => {
    state.dialog = false
  },
  saveProject (state) {
    post('/project/',
      state.fields
    ).then((response) => {
      state.dialog = false
    }).catch((error) => {
      if (error.response.data) {
        let datas = error.response.data
        for (var name in datas) {
          state.msg[name] = datas[name]
          console.log(state.msg)
        }
      }
    })
  },
  newProjectDialog (state, payload) {
    payload.reset()
    let msg = state.msg
    for (var i in msg) {
      msg[i] = ''
    }
  },
  loadProjectSelect (state) {
    state.env = this._vm.$data.$$state.listproject.env
    state.parent = this._vm.$data.$$state.listproject.parent
    state.project_nature = this._vm.$data.$$state.listproject.project_nature
    state.projectType = this._vm.$data.$$state.listproject.projectType
    state.status = this._vm.$data.$$state.listproject.status
    state.publish_type = this._vm.$data.$$state.listproject.publish_type
    state.owner = this._vm.$data.$$state.listproject.owner
    state.build_param = this._vm.$data.$$state.listproject.build_param
    state.pub_version = this._vm.$data.$$state.listproject.pub_version
    state.detection = this._vm.$data.$$state.listproject.detection
  },
  watchPublishType (state) {
    state.fields.pubcmd = null
    if (state.fields.publish_type === 1) {
      state.showPubcmd = true
      state.showJavaopt = true
      this.commit('newproject/projectJavaopt')
      if (state.fields.env) {
        this.commit('newproject/projectGetPubcmd')
      }
    } else {
      state.showPubcmd = false
      state.showJavaopt = false
    }
  },
  projectGetHost (state) {
    state.fields.host = []
    state.host = {items: [{ text: '', value: '' }]}
    fetch('/project/get_host/', {
      env: state.fields.env
    }).then((response) => {
      let res = JSON.parse(response)
      state.host.items = res.data
    }).catch((error) => {
      state.etext = error.response
    })
  },
  projectGetService (state) {
    state.fields.service = []
    state.service = {items: [{ text: '', value: '' }]}
    fetch('/project/get_service/', {
      env: state.fields.env
    }).then((response) => {
      let res = JSON.parse(response)
      state.service.items = res.data
    }).catch((error) => {
      state.etext = error.response
    })
  },
  projectJavaopt (state) {
    state.fields.javaopt = null
    state.javaopt = {items: [{text: '', value: ''}]}
    fetch('/project/get_javaopt/', {

    }).then((response) => {
      let res = JSON.parse(response)
      state.javaopt.items = res.data
    }).catch((error) => {
      state.etext = error.response
    })
  },
  projectGetPubcmd (state) {
    state.fields.pubcmd = null
    state.pubcmd = {items: [{text: '', value: ''}]}
    fetch('/project/get_pubcmd/', {
      env: state.fields.env
    }).then((response) => {
      let res = JSON.parse(response)
      state.pubcmd.items = res.data
    }).catch((error) => {
      state.etext = error.response
    })
  },
  getDockerOpt (state) {
    state.fields.dockeropt = null
    state.dockeropt = {items: [{text: '', value: ''}]}
    fetch('/project/get_dockeropt/', {

    }).then((response) => {
      let res = JSON.parse(response)
      state.dockeropt.items = res.data
    }).catch((error) => {
      state.etext = error.response
    })
  },
  getDockerCmd (state) {
    state.fields.dockercmd = null
    state.dockercmd = {items: [{text: '', value: ''}]}
    fetch('/project/get_dockercmd/', {
      env: state.fields.env
    }).then((response) => {
      let res = JSON.parse(response)
      state.dockercmd.items = res.data
    }).catch((error) => {
      state.etext = error.response
    })
  },
  watchPubVersion (state) {
    switch (state.fields.pub_version) {
      case 0:
        state.showBuildParam = false
        state.showDockeropt = false
        state.showDockercmd = false
        state.fields.docker_opt = ''
        state.fields.docker_cmd = ''
        break
      case 1:
        state.showBuildParam = true
        state.showDockeropt = false
        state.showDockercmd = false
        state.fields.docker_opt = ''
        state.fields.docker_cmd = ''
        break
      case 2:
        state.showBuildParam = true
        state.showDockeropt = true
        state.showDockercmd = true
        this.commit('newproject/getDockerOpt')
        if (state.fields.env) {
          this.commit('newproject/getDockerCmd')
        }
    }
  }
}

const actions = {
  closeNewProject ({commit}) {
    commit('closeNewProject')
  },
  saveProject ({commit}, payload) {
    if (payload.validate()) {
      commit('saveProject')
    }
  },
  newProjectDialog ({commit, state}, payload) {
    commit('newProjectDialog', payload)
    if (!state.dialog) {
      this.dispatch('listproject/handlerProject')
      this.dispatch('listproject/loadProjectFilters')
    }
  },
  loadProjectSelect ({commit}) {
    commit('loadProjectSelect')
  },
  watchPublishType ({commit}) {
    commit('watchPublishType')
    commit('projectJavaopt')
  },
  watchProjectEnv ({commit}) {
    commit('projectGetHost')
    commit('projectGetService')
    if (state.showPubcmd && state.fields.publish_type === 1) {
      commit('projectGetPubcmd')
    }
    if (state.showDockercmd) {
      commit('getDockerCmd')
    }
  },
  watchPubVersion ({commit}) {
    commit('watchPubVersion')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
